import { Inject, Controller, Get, Post, Body, Query} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { TransferService } from '../../service/user/transfer.service';
import { Transfer } from '../../entity/user/transfer';
import { PetService } from '../../service/user/pet.service';
import * as fs from 'fs';
import * as path from 'path';


@Controller('/api/user')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  transferService: TransferService;
  @Inject()
  petService: PetService;

  @Get('/transfer', { description: '获得转养表' })
  async gettransfer(@Query('ownerid') ownerid: string) {
    const result = await this.transferService.gettransfer(ownerid)
    console.log("result.length",result.length);
    if(result.length!=0){
      for(var i = 0 ;i< result.length; i++){
        const petinfo = await this.petService.petinfo(result[i].petid)
        console.log("petinfo",petinfo);
        const fileName = petinfo.petpic.imageurl;
        console.log("fileName",fileName);
        const petpic = fs.readFileSync(path.join(__dirname, '../../../public/upload', fileName));
        result[i]["src"] = petpic
        result[i]["petinfo"] = petinfo 
        result[i]["type"] = "转养"      
      } 
    }
    return { success: true, message: '转让查询成功', data: { result } }
  }

  @Post('/transfer', { description: '上传转让' })
  async transfer(@Body() transfer: Transfer) {
    console.log(transfer);
    const result = await this.transferService.transfer(transfer)
    return { success: true, message: '转让提交成功', data: { result } }
  }

 
}